#define NMAX 500000

extern struct Pipe {
	int data[NMAX];
	int head;
	int size;
};


extern void
Clear(struct Pipe * p)
{
	p->size = 0;
	p->head = 0;
}



extern int
Push(struct Pipe * p, int key)
{
	if (p->head + p->size - NMAX >= NMAX)
		return -1;
	p->data[(p->head + p->size) % NMAX] = key;
	++p->size;
	return 0;
}



extern int
Pop(struct Pipe * p)
{
	int ret = p->data[p->head];
	if (++p->head == NMAX)
		p->head = 0;
	--p->size;
	return ret;
}



extern int
Head(const struct Pipe * p)
{
	return p->data[p->head];
}



extern int
Tail(const struct Pipe * p)
{
	return p->data[(p->head + p->size) % NMAX];
}



extern unsigned char
IsEmpty(const struct Pipe * p)
{
	return p->size ? 0 : 1;
}
